/**
 * 
 */
package com.yidu.user.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yidu.model.ManagePayIn;
import com.yidu.model.ManageUser;
import com.yidu.model.Page;
import com.yidu.model.Plot;
import com.yidu.user.dao.ManagePayInMapper;
import com.yidu.user.dao.ManageUserMapper;

@Service(value="payInService")
public class MangePayInServiceImpl implements MangePayInService{
	@Resource
	private ManagePayInMapper payInMapper;
	@Resource
	private ManageUserMapper userMapper;
	/**
	 * 查询所有信息
	 */
	@Override
	public List<ManagePayIn> getAllPayIn(Page page,ManagePayIn pay) {
		if(page.getPages()==0){
			page.setRows(10);
			page.setPages(1);
		}
		if(page.getTotal()==0){
			if(page.getUserId()==null || "".equals(page.getUserId())){
				page.setTotal(getAllCount(pay.getUsName()));
			}else{
				ManageUser us = userMapper.getIdByName(page.getUserId());
				pay.setUsName(us.getUserId());
				page.setTotal(getAllCount(pay.getUsName()));
			}
		}
		
		if(page.getUserId()==null || "".equals(page.getUserId())){
			page.setUserId(pay.getUsName());
			ManageUser us = userMapper.getIdByName(page.getUserId());
			page.setUserId(us.getUserId());
			List<ManagePayIn> list = payInMapper.getAllPayIn(page);
			return list;
		}else{
			ManageUser us = userMapper.getIdByName(page.getUserId());
			page.setUserId(us.getUserId());
			List<ManagePayIn> list = payInMapper.getAllPayIn(page);
			return list;
		}
		
	}
	@Override
	public int deleteByPayInId(String payinId,String usName) {
		//查询
		ManagePayIn pay = payInMapper.showUpdate(payinId);
		//查询出该用户信息
		ManageUser us = userMapper.getIdByName(usName);
		System.out.println(usName);
		//修改钱数，
		us.setUserBalance(us.getUserBalance()-pay.getPayinMoney());
		System.out.println(us.getUserBalance()+"=======");
		//修改用户信息
		userMapper.updateByPrimaryKeySelective(us);
		int row = payInMapper.deleteByPayInId(payinId);
		
		return row;
	}
	@Override
	public ManagePayIn showUpdate(String payinId) {
		
		return payInMapper.showUpdate(payinId);
	}
	@Override
	public int updateByPayInId(ManagePayIn payIn) {
		//修改
		if(!"".equals(payIn.getPayinId())){
			//查询出修改前的钱
			ManagePayIn pay = payInMapper.showUpdate(payIn.getPayinId());
			//查询出该用户信息
			ManageUser us = userMapper.getIdByName(payIn.getUsName());
			//修改钱数，先减掉查询出的钱数再增加修改后的钱
			us.setUserBalance(us.getUserBalance()-pay.getPayinMoney()+payIn.getPayinMoney());
			//修改用户信息
			userMapper.updateByPrimaryKeySelective(us);
			//修改之处信息
			int row = payInMapper.updateByPayInId(payIn);
			return row;
		}else{
			//增加
			//之处主键
			payIn.setPayinId(UUID.randomUUID().toString().replaceAll("-", ""));
			//查询出该用户信息
			ManageUser us = userMapper.getIdByName(payIn.getUsName());
			//增加用户钱
			us.setUserBalance(us.getUserBalance()+payIn.getPayinMoney());
			//修改用户信息
			userMapper.updateByPrimaryKeySelective(us);
			
			payIn.setUser(us);
			//设置当前时间
			Date date = new Date();
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time = sf.format(date);
			payIn.setPayinDate(time);
			//增加收入详情
			int row = payInMapper.addPayIn(payIn);
			return row;
		}
		
	}
	
	@Override
	public int getAllCount(String userId) {
		return payInMapper.getAllCount(userId);
	}
	@Override
	public List<Plot> selectMoney(String id) {
		
		return payInMapper.selectMoney(id);
	}
	
}
